package com.bwie.service.impl;

import com.bwie.exception.BusinessException;
import com.bwie.mapper.RegistrationMapper;
import com.bwie.mapper.UserMapper;
import com.bwie.mq.MessageProduce;
import com.bwie.pojo.Result;
import com.bwie.pojo.dto.RegistrationDto;
import com.bwie.pojo.po.DictPo;
import com.bwie.pojo.po.RegistrationPo;
import com.bwie.pojo.po.UserPo;
import com.bwie.pojo.vo.RegistrationVo;
import com.bwie.service.RegistrationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ 挂号
 * @descriptions: RegistrationServiceImpl
 * @author: LiYang
 * @date: 2025/3/14 15:42
 * @version: 1.0
 */
@Service
public class RegistrationServiceImpl implements RegistrationService {
    @Autowired
    private RegistrationMapper registrationMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MessageProduce messageProduce;

    @Override
    public Result getList(RegistrationDto registrationDto) {
        PageHelper.startPage(registrationDto.getPageNum(), registrationDto.getPageSize());
        UserPo user = (UserPo) request.getSession().getAttribute("user");
        List<RegistrationVo> list = null;
        if (user.getUserGrade() == 0) {
            list = registrationMapper.getUserList(user.getUserGrade(),registrationDto.getState());
        } else {
            list = registrationMapper.getList(user.getUserId(), user.getUserGrade(),registrationDto.getState());
        }

        PageInfo<RegistrationVo> pageInfo = new PageInfo<>(list);
        return Result.success("查询成功",pageInfo);
    }

    @Override
    @Transactional
    public Result getAdd(RegistrationVo registrationVo) {
        String patientName = registrationVo.getPatientName();
        UserPo userPo = userMapper.selectName(patientName);
        if(userPo==null){
            UserPo user = new UserPo();
            user.setUserGrade(0);
            user.setAge(registrationVo.getAge());
            user.setUserName(registrationVo.getPatientName());
            user.setUserPhone(registrationVo.getPhoneNumber());
            user.setSex(registrationVo.getSex());
            user.setBirthDate(registrationVo.getBirthDate());
            user.setIdCard(registrationVo.getIdCard());

            userMapper.add(user);
        }
//        DictPo dictPo=registrationMapper.selectDict();
//        if(dictPo.getDictValue()==registrationVo.getState()){
//            registrationVo.setState(dictPo.getDictValue());
//        }
        if(StringUtils.isEmpty(registrationVo.getPatientName())){
            throw new BusinessException("患者姓名为空");
        }
        if(StringUtils.isEmpty(registrationVo.getPhoneNumber())){
            throw new BusinessException("手机号为空");
        }
        if(registrationVo.getBirthDate()==null){
            throw new BusinessException("出生日期为空");
        }
        registrationVo.setRegistrationType(0);
        registrationMapper.add(registrationVo);
        return Result.success("挂号成功");
    }

    @Override
    public RegistrationVo selectId(Integer registrationId) {
        return registrationMapper.selectId(registrationId);
    }

    @Override
    @Transactional
    public Result toOut(RegistrationPo registrationPo) {
        //先更新状态并且修改退号原因
        registrationMapper.upaRemarks(registrationPo);
        registrationMapper.upd(registrationPo);
        //发送短信
        RegistrationPo registration=registrationMapper.getID(registrationPo.getRegistrationId());
        messageProduce.sendMes(registration);
        return Result.success("退号成功");
    }
}
